package com.fhd.service.Bill;

import com.fhd.dao.BaseDao;
import com.fhd.dao.Bill.BillDao;
import com.fhd.dao.Bill.BillDaoImplement;
import com.fhd.dao.Client.ClientDao;
import com.fhd.dao.Client.ClientDaoImplement;
import com.fhd.dao.House.HouseDao;
import com.fhd.dao.House.HouseDaoImplement;
import com.fhd.dao.Merchant.MerchantDao;
import com.fhd.dao.Merchant.MerchantDaoImplement;
import com.fhd.pojo.Bill;
import com.fhd.pojo.House;
import com.fhd.pojo.Merchant;
import com.fhd.pojo.Recharge;
import com.fhd.service.Client.ClientService;
import com.fhd.service.Merchant.MerchantService;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;

public class BillServiceImplement implements BillService{
    ClientDao clientDao = null;
    MerchantDao merchantDao = null;
    BillDao billDao = null;
    HouseDao houseDao = null;

    public BillServiceImplement() {
        clientDao = new ClientDaoImplement();
        merchantDao = new MerchantDaoImplement();
        billDao = new BillDaoImplement();
        houseDao = new HouseDaoImplement();
    }

    // 客户下单
    @Override
    public int clientPlaceOrder(int clientId, int merchantId, int houseId, int money, int number, Date startTime, Date endTime) throws SQLException {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            // 开启事务
            conn.setAutoCommit(false);
            // 执行所有sql语句（客户扣钱，商户加钱加销量，房型减数量,创建订单）
            int row1 = clientDao.updateMoney(conn, clientId, -money);
            int row2 = merchantDao.updateMoney(conn, merchantId, money);
            int row3 = merchantDao.updateSoldNumber(conn, merchantId, number);
            int row4 = houseDao.updateRemainNumber(conn, houseId, -number);
            int row5 = billDao.createBill(conn, clientId, merchantId, houseId, money,startTime, endTime, number);
            affectedRow = row1 + row2 + row3 + row4 + row5;
        } catch (Exception e) {
            if (conn != null) {
                conn.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                conn.commit();
            }
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 取消订单业务
    @Override
    public int cancelBill(int id, int clientId,int merchantId,int houseId,int money, int number,int situation) throws SQLException {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            // 开启事务
            conn.setAutoCommit(false);
            // 执行所有sql语句（客户加钱，商户扣钱扣销量, 房型加数量,订单修改状态）
            int row1 = clientDao.updateMoney(conn, clientId, money);
            int row2 = merchantDao.updateMoney(conn, merchantId, -money);
            int row3 = merchantDao.updateSoldNumber(conn, merchantId, -number);
            int row4 = houseDao.updateRemainNumber(conn, houseId, number);
            int row5 = billDao.updateSituation(conn,id,situation);
            affectedRow = row1 + row2 + row3 + row4 + row5;
        } catch (Exception e) {
            if (conn != null) {
                conn.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                conn.commit();
            }
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 获取某个用户的所有订单
    @Override
    public ArrayList<Bill> getClientBill(int clientId) {
        Connection conn = null;
        ArrayList<Bill> bills = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            bills = billDao.getClientBill(conn, clientId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return bills;
    }

    // 获取某个商户的所有订单
    @Override
    public ArrayList<Bill> getMerchantBill(int merchantId) {
        Connection conn = null;
        ArrayList<Bill> bills = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            bills = billDao.getMerchantBill(conn, merchantId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return bills;
    }

    // 更改订单状态
    @Override
    public int updateBillSituation(int id, int situation) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            // 执行所有sql语句
            affectedRow = billDao.updateSituation(conn,id,situation);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

}
